﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Backend_Order_OrderDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		this.Form.Action = "Backend/Order/OrderDetail.aspx";
		// check if we must display order details
		if (!Page.IsPostBack && Request.QueryString["OrderID"] != null)
		{
			string orderId = Request.QueryString["OrderID"];
			// fill constituent controls with data
			PopulateControls(orderId);
			// set edit mode
			SetEditMode(false);
		}
    }

	// populate the form with data
	private void PopulateControls(string orderId)
	{
		Session["OrderId"] = orderId;
		// obtain order info
		OrderInfo orderInfo = EF_OrderAccess.GetInfo(orderId);
		// populate labels and text boxes with order info
		orderIdLabel.Text = "ទិន្នន័យនៃការបញ្ជាទិញលេខ៖ #" + orderId;
		totalAmountLabel.Text = String.Format("{0:c}", orderInfo.TotalAmount);
		dateCreatedTextBox.Text = orderInfo.OrderDate;
		dateShippedTextBox.Text = orderInfo.DateShipped;
		verifiedCheck.Checked = orderInfo.Verified;
		completedCheck.Checked = orderInfo.Completed;
		canceledCheck.Checked = orderInfo.Canceled;
		commentsTextBox.Text = orderInfo.Comments;
		customerNameTextBox.Text = orderInfo.CustomerName;
		customerPhoneTextBox.Text = orderInfo.CustomerPhone;
		shippingAddressTextBox.Text = orderInfo.ShippingAddress;
		customerEmailTextBox.Text = orderInfo.CustomerEmail;
		// by default the Edit button is enabled, and the
		// Update and Cancel buttons are disabled
		editButton.Enabled = true;
		updateButton.Enabled = false;
		cancelButton.Enabled = false;
		// Decide which one of the other three buttons
		// should be enabled and which should be disabled
		if (canceledCheck.Checked || completedCheck.Checked)
		{
			// if the order was canceled or completed ...
			markVerifiedButton.Enabled = false;
			markCompletedButton.Enabled = false;
			markCanceledButton.Enabled = false;
		}
		else if (verifiedCheck.Checked)
		{
			// if the order was not canceled but is verified ...
			markVerifiedButton.Enabled = false;
			markCompletedButton.Enabled = true;
			markCanceledButton.Enabled = true;
		}
		else
		{
			// if the order was not canceled and is not verified
			markVerifiedButton.Enabled = true;
			markCompletedButton.Enabled = false;
			markCanceledButton.Enabled = true;
		}

		// fill the data grid with order details
		grid.DataSource = EF_OrderAccess.GetDetails(orderId);
		grid.DataBind();
	}

	// enable or disable edit mode
	private void SetEditMode(bool enable)
	{
		dateCreatedTextBox.Enabled = enable;
		dateShippedTextBox.Enabled = enable;
		verifiedCheck.Enabled = enable;
		completedCheck.Enabled = enable;
		canceledCheck.Enabled = enable;
		commentsTextBox.Enabled = enable;
		customerNameTextBox.Enabled = enable;
		customerPhoneTextBox.Enabled = enable;
		shippingAddressTextBox.Enabled = enable;
		customerEmailTextBox.Enabled = enable;
		editButton.Enabled = !enable;
		updateButton.Enabled = enable;
		cancelButton.Enabled = enable;
	}

	// enter edit mode
	protected void editButton_Click(object sender, EventArgs e)
	{
		string orderId = Session["OrderId"].ToString();
		PopulateControls(orderId);
		SetEditMode(true);
	}

	// cancel edit mode
	protected void cancelButton_Click(object sender, EventArgs e)
	{
		string orderId = Session["OrderId"].ToString();
		PopulateControls(orderId);
	}

	// update order information
	protected void updateButton_Click(object sender, EventArgs e)
	{
		// Store the new order details in an OrderInfo object
		OrderInfo orderInfo = new OrderInfo();
		string orderId = Session["OrderId"].ToString();
		orderInfo.OrderID = Int32.Parse(orderId);
		orderInfo.OrderDate = dateCreatedTextBox.Text;
		orderInfo.DateShipped = dateShippedTextBox.Text;
		orderInfo.Verified = verifiedCheck.Checked;
		orderInfo.Completed = completedCheck.Checked;
		orderInfo.Canceled = canceledCheck.Checked;
		orderInfo.Comments = commentsTextBox.Text;
		orderInfo.CustomerName = customerNameTextBox.Text;
		orderInfo.CustomerPhone = customerPhoneTextBox.Text;
		orderInfo.ShippingAddress = shippingAddressTextBox.Text;
		orderInfo.CustomerEmail = customerEmailTextBox.Text;
		// try to update the order 
		try
		{
			// Update the order
			EF_OrderAccess.Update(orderInfo);
		}
		catch (Exception ex)
		{
			// In case of an error, we simply ignore it
		}
		// Exit edit mode 
		SetEditMode(false);
		// Update the form
		PopulateControls(orderId);
	}

	// mark order as verified
	protected void markVerifiedButton_Click(object sender, EventArgs e)
	{
		// obtain the order ID from the query string
		string orderId = Session["OrderId"].ToString();
		// mark order as verified
		EF_OrderAccess.MarkVerified(orderId);
		// update the form
		PopulateControls(orderId);
	}

	// mark order as completed
	protected void markCompletedButton_Click(object sender, EventArgs e)
	{
		// obtain the order ID from the query string
		string orderId = Session["OrderId"].ToString();
		// mark the order as completed
		EF_OrderAccess.MarkCompleted(orderId);
		// update the form
		PopulateControls(orderId);
	}

	// mark order as canceled
	protected void markCanceledButton_Click(object sender, EventArgs e)
	{
		// obtain the order ID from the query string
		string orderId = Session["OrderId"].ToString();
		// mark the order as canceled
		EF_OrderAccess.MarkCanceled(orderId);
		// update the form
		PopulateControls(orderId);
	}
}